package com.itheima.day4.analysis;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.format.DateTimeFormatter;
import java.util.AbstractQueue;
import java.util.Comparator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.stream.Stream;

import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;

public class AnalysisTest04 {
    /*
        数据格式
        0       1       2       3       4       5       6     7      8        9     10    11
        序号    下单时间  订单编号  商品编号 类别编号  类别码   品牌   价格   用户编号   年龄   性别   地区
     */
    static final int INDEX = 0;
    static final int TIME = 1;
    static final int ORDER_ID = 2;
    static final int PRODUCT_ID = 3;
    static final int CATEGORY_ID = 4;
    static final int CATEGORY_CODE = 5;
    static final int BRAND = 6;
    static final int PRICE = 7;
    static final int USER_ID = 8;
    static final int USER_AGE = 9;
    static final int USER_SEX = 10;
    static final int USER_REGION = 11;
    static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");

    public static void main(String[] args) {
        case42();
    }

    // 4) 下单最多的前十名用户
    private static void case41() {
        try (Stream<String> lines = Files.lines(Path.of("./data.txt"))) {
            Map<String, Long> collect = lines.skip(1)
                    .map(line -> line.split(","))
                    .collect(groupingBy(array -> array[USER_ID], counting()));
//            for (Map.Entry<String, Long> e : collect.entrySet()) {
//                System.out.println(e);M
//            }
            collect.entrySet().stream()
                    .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
                    .limit(10)
                    .forEach(System.out::println);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static class MyQueue<E> extends PriorityQueue<E> {
        private final int max;

        public MyQueue(Comparator<? super E> comparator, int max) {
            super(comparator);
            this.max = max;
        }

        @Override
        public boolean offer(E e) {
            boolean r = super.offer(e);
            if (this.size() > max) {
                this.poll();
            }
            return r;
        }
    }

    private static void case42() {
        try (Stream<String> lines = Files.lines(Path.of("./data.txt"))) {
            Map<String, Long> collect = lines.skip(1)
                    .map(line -> line.split(","))
                    .collect(groupingBy(array -> array[USER_ID], counting()));

            MyQueue<Map.Entry<String, Long>> queue = collect
                    .entrySet().stream()
                    .collect(
                            () -> new MyQueue<>(Map.Entry.comparingByValue(), 10),
                            MyQueue::offer,
                            AbstractQueue::addAll
                    );

            while (!queue.isEmpty()) {
                System.out.println(queue.poll());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
